﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Character;
using IllusionUtility.GetUtility;
using RootMotion;
using RootMotion.FinalIK;
using SEXY;
using UnityEngine;

namespace Studio
{
	// Token: 0x02000461 RID: 1121
	public static class AddObjectAssist
	{
		// Token: 0x06001C02 RID: 7170 RVA: 0x000B3A5C File Offset: 0x000B1C5C
		public static void InitBone(OCIChar _ociChar, Transform _transformRoot, Dictionary<int, Info.BoneInfo> _dicBoneInfo)
		{
			foreach (KeyValuePair<int, Info.BoneInfo> keyValuePair in _dicBoneInfo)
			{
				GameObject gameObject = null;
				switch (keyValuePair.Value.group)
				{
				case 7:
				case 8:
				case 9:
				{
					HAIR_TYPE[] array = new HAIR_TYPE[]
					{
						HAIR_TYPE.FRONT,
						HAIR_TYPE.BACK,
						HAIR_TYPE.SIDE
					};
					GameObject obj = _ociChar.charInfo.human.hairs.GetObj(array[keyValuePair.Value.group - 7]);
					gameObject = ((!(obj != null)) ? null : obj.transform.FindLoop(keyValuePair.Value.bone));
					break;
				}
				default:
					gameObject = _transformRoot.FindLoop(keyValuePair.Value.bone);
					if (gameObject == null)
					{
						GlobalData.Debug_LogWarning(keyValuePair.Value.bone);
					}
					break;
				}
				if (!(gameObject == null))
				{
					OIBoneInfo oiboneInfo = null;
					if (!_ociChar.oiCharInfo.bones.TryGetValue(keyValuePair.Key, out oiboneInfo))
					{
						oiboneInfo = new OIBoneInfo(Studio.GetNewIndex());
						_ociChar.oiCharInfo.bones.Add(keyValuePair.Key, oiboneInfo);
					}
					switch (keyValuePair.Value.group)
					{
					case 0:
					case 1:
					case 2:
					case 3:
					case 4:
						oiboneInfo.group = (OIBoneInfo.BoneGroup)(1 << keyValuePair.Value.group | 1);
						break;
					case 5:
					case 6:
						goto IL_1E6;
					case 7:
					case 8:
					case 9:
						oiboneInfo.group = OIBoneInfo.BoneGroup.Hair;
						break;
					case 10:
						oiboneInfo.group = OIBoneInfo.BoneGroup.Neck;
						break;
					case 11:
					case 12:
						oiboneInfo.group = OIBoneInfo.BoneGroup.Breast;
						break;
					case 13:
						oiboneInfo.group = OIBoneInfo.BoneGroup.Skirt;
						break;
					default:
						goto IL_1E6;
					}
					IL_203:
					oiboneInfo.level = keyValuePair.Value.level;
					GuideObject guideObject = global::Singleton<GuideObjectManager>.Instance.Add(gameObject.transform, oiboneInfo.dicKey);
					guideObject.enablePos = false;
					guideObject.enableScale = false;
					guideObject.enableMaluti = false;
					guideObject.calcScale = false;
					guideObject.scaleRate = 0.5f;
					guideObject.scaleRot = 0.025f;
					guideObject.scaleSelect = 0.05f;
					guideObject.parentGuide = _ociChar.guideObject;
					_ociChar.listBones.Add(new OCIChar.BoneInfo(guideObject, oiboneInfo));
					guideObject.SetActive(false, true);
					continue;
					IL_1E6:
					oiboneInfo.group = (OIBoneInfo.BoneGroup)(1 << keyValuePair.Value.group);
					goto IL_203;
				}
			}
		}

		// Token: 0x06001C03 RID: 7171 RVA: 0x000B3D44 File Offset: 0x000B1F44
		public static void InitHairBone(OCIChar _ociChar, Dictionary<int, Info.BoneInfo> _dicBoneInfo)
		{
			foreach (KeyValuePair<int, Info.BoneInfo> keyValuePair in from b in _dicBoneInfo
			where MathfEx.RangeEqualOn<int>(7, b.Value.@group, 9)
			select b)
			{
				HAIR_TYPE[] array = new HAIR_TYPE[]
				{
					HAIR_TYPE.FRONT,
					HAIR_TYPE.BACK,
					HAIR_TYPE.SIDE
				};
				GameObject obj = _ociChar.charInfo.human.hairs.GetObj(array[keyValuePair.Value.group - 7]);
				GameObject gameObject = (!(obj != null)) ? null : obj.transform.FindLoop(keyValuePair.Value.bone);
				if (!(gameObject == null))
				{
					OIBoneInfo oiboneInfo = null;
					if (!_ociChar.oiCharInfo.bones.TryGetValue(keyValuePair.Key, out oiboneInfo))
					{
						oiboneInfo = new OIBoneInfo(Studio.GetNewIndex());
						_ociChar.oiCharInfo.bones.Add(keyValuePair.Key, oiboneInfo);
					}
					oiboneInfo.group = OIBoneInfo.BoneGroup.Hair;
					oiboneInfo.level = keyValuePair.Value.level;
					GuideObject guideObject = global::Singleton<GuideObjectManager>.Instance.Add(gameObject.transform, oiboneInfo.dicKey);
					guideObject.enablePos = false;
					guideObject.enableScale = false;
					guideObject.enableMaluti = false;
					guideObject.scaleRate = 0.5f;
					guideObject.scaleRot = 0.025f;
					guideObject.scaleSelect = 0.05f;
					guideObject.parentGuide = _ociChar.guideObject;
					_ociChar.listBones.Add(new OCIChar.BoneInfo(guideObject, oiboneInfo));
					guideObject.SetActive(false, true);
				}
			}
		}

		// Token: 0x06001C04 RID: 7172 RVA: 0x000B3F18 File Offset: 0x000B2118
		public static void ResetHairTransfrom(OCIChar _ociChar, Dictionary<int, Info.BoneInfo> _dicBoneInfo)
		{
			foreach (KeyValuePair<int, Info.BoneInfo> keyValuePair in from b in _dicBoneInfo
			where MathfEx.RangeEqualOn<int>(7, b.Value.@group, 9)
			select b)
			{
				OIBoneInfo bInfo = null;
				if (_ociChar.oiCharInfo.bones.TryGetValue(keyValuePair.Key, out bInfo))
				{
					HAIR_TYPE[] array = new HAIR_TYPE[]
					{
						HAIR_TYPE.FRONT,
						HAIR_TYPE.BACK,
						HAIR_TYPE.SIDE
					};
					GameObject obj = _ociChar.charInfo.human.hairs.GetObj(array[keyValuePair.Value.group - 7]);
					GameObject gameObject = (!(obj != null)) ? null : obj.transform.FindLoop(keyValuePair.Value.bone);
					if (!(gameObject == null))
					{
						OCIChar.BoneInfo boneInfo = _ociChar.listBones.Find((OCIChar.BoneInfo i) => i.boneInfo == bInfo);
						if (boneInfo != null)
						{
							boneInfo.guideObject.transformTarget = gameObject.transform;
						}
					}
				}
			}
		}

		// Token: 0x06001C05 RID: 7173 RVA: 0x000B4068 File Offset: 0x000B2268
		public static void InitIKTarget(OCIChar _ociChar, bool _addInfo)
		{
			FullBodyBipedIK fullBodyIK = _ociChar.fullBodyIK;
			IKSolverFullBodyBiped solver = fullBodyIK.solver;
			BipedReferences references = fullBodyIK.references;
			_ociChar.ikCtrl = _ociChar.charInfo.gameObject.AddComponent<IKCtrl>();
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 0, solver.bodyEffector, false, references.pelvis);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 1, solver.leftShoulderEffector, false, references.leftUpperArm);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 2, solver.leftArmChain, false, references.leftForearm);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 3, solver.leftHandEffector, true, references.leftHand);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 4, solver.rightShoulderEffector, false, references.rightUpperArm);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 5, solver.rightArmChain, false, references.rightForearm);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 6, solver.rightHandEffector, true, references.rightHand);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 7, solver.leftThighEffector, false, references.leftThigh);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 8, solver.leftLegChain, false, references.leftCalf);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 9, solver.leftFootEffector, true, references.leftFoot);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 10, solver.rightThighEffector, false, references.rightThigh);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 11, solver.rightLegChain, false, references.rightCalf);
			AddObjectAssist.AddIKTarget(_ociChar, _ociChar.ikCtrl, 12, solver.rightFootEffector, true, references.rightFoot);
			if (_addInfo)
			{
				_ociChar.ikCtrl.InitTarget();
			}
		}

		// Token: 0x06001C06 RID: 7174 RVA: 0x000B4208 File Offset: 0x000B2408
		public static void InitAccessoryPoint(OCIChar _ociChar)
		{
			ExcelData excelData = global::Singleton<Info>.Instance.accessoryPointGroup;
			int count = excelData.list.Count;
			Dictionary<int, TreeNodeObject> dictionary = new Dictionary<int, TreeNodeObject>();
			for (int i = 1; i < count; i++)
			{
				ExcelData.Param param = excelData.list[i];
				int key = int.Parse(param.list[0]);
				string arg = param.list[1];
				TreeNodeObject treeNodeObject = Studio.AddNode(string.Format("グループ : {0}", arg), _ociChar.treeNodeObject);
				treeNodeObject.treeState = ((!_ociChar.oiCharInfo.dicAccessGroup.ContainsKey(key)) ? TreeNodeObject.TreeState.Close : _ociChar.oiCharInfo.dicAccessGroup[key]);
				treeNodeObject.enableChangeParent = false;
				treeNodeObject.enableDelete = false;
				treeNodeObject.enableCopy = false;
				dictionary.Add(key, treeNodeObject);
				_ociChar.dicAccessPoint.Add(key, new OCIChar.AccessPointInfo(treeNodeObject));
			}
			excelData = global::Singleton<Info>.Instance.accessoryPoint;
			count = excelData.list.Count;
			for (int j = 1; j < count; j++)
			{
				ExcelData.Param param2 = excelData.list[j];
				int num = int.Parse(param2.list[0]);
				int key2 = int.Parse(param2.list[1]);
				string arg2 = param2.list[3];
				TreeNodeObject parent = dictionary[key2];
				TreeNodeObject treeNodeObject2 = Studio.AddNode(string.Format("部位 : {0}", arg2), parent);
				treeNodeObject2.treeState = ((!_ociChar.oiCharInfo.dicAccessNo.ContainsKey(num)) ? TreeNodeObject.TreeState.Close : _ociChar.oiCharInfo.dicAccessNo[num]);
				treeNodeObject2.enableChangeParent = false;
				treeNodeObject2.enableDelete = false;
				treeNodeObject2.enableCopy = false;
				treeNodeObject2.baseColor = Utility.ConvertColor(204, 128, 164);
				treeNodeObject2.colorSelect = treeNodeObject2.baseColor;
				_ociChar.dicAccessoryPoint.Add(treeNodeObject2, num);
				OCIChar.AccessPointInfo accessPointInfo = null;
				if (_ociChar.dicAccessPoint.TryGetValue(key2, out accessPointInfo))
				{
					accessPointInfo.child.Add(num, treeNodeObject2);
				}
			}
			foreach (KeyValuePair<int, TreeNodeObject> keyValuePair in dictionary)
			{
				keyValuePair.Value.enableAddChild = false;
			}
			global::Singleton<Studio>.Instance.treeNodeCtrl.RefreshHierachy();
		}

		// Token: 0x06001C07 RID: 7175 RVA: 0x000B449C File Offset: 0x000B269C
		public static void LoadChild(List<ObjectInfo> _child, ObjectCtrlInfo _parent, TreeNodeObject _parentNode)
		{
			foreach (ObjectInfo child in _child)
			{
				AddObjectAssist.LoadChild(child, _parent, _parentNode);
			}
		}

		// Token: 0x06001C08 RID: 7176 RVA: 0x000B44F4 File Offset: 0x000B26F4
		public static void LoadChild(Dictionary<int, ObjectInfo> _child, ObjectCtrlInfo _parent = null, TreeNodeObject _parentNode = null)
		{
			foreach (KeyValuePair<int, ObjectInfo> keyValuePair in _child)
			{
				AddObjectAssist.LoadChild(keyValuePair.Value, _parent, _parentNode);
			}
		}

		// Token: 0x06001C09 RID: 7177 RVA: 0x000B4554 File Offset: 0x000B2754
		public static void LoadChild(ObjectInfo _child, ObjectCtrlInfo _parent = null, TreeNodeObject _parentNode = null)
		{
			switch (_child.kind)
			{
			case 0:
			{
				OICharInfo oicharInfo = _child as OICharInfo;
				if (oicharInfo.sex == 1)
				{
					AddObjectFemale.Load(oicharInfo, _parent, _parentNode);
				}
				else
				{
					AddObjectMale.Load(oicharInfo, _parent, _parentNode);
				}
				break;
			}
			case 1:
				AddObjectItem.Load(_child as OIItemInfo, _parent, _parentNode);
				break;
			case 2:
				AddObjectLight.Load(_child as OILightInfo, _parent, _parentNode);
				break;
			case 3:
				AddObjectFolder.Load(_child as OIFolderInfo, _parent, _parentNode);
				break;
			}
		}

		// Token: 0x06001C0A RID: 7178 RVA: 0x000B45EC File Offset: 0x000B27EC
		public static void InitHandAnime(OCIChar _ociChar)
		{
			int sex = _ociChar.oiCharInfo.sex;
			GameObject[] array = (sex != 1) ? global::Singleton<Studio>.Instance.prefabMaleHand : global::Singleton<Studio>.Instance.prefabFemaleHand;
			string[,] array2 = new string[2, 2];
			array2[0, 0] = "cm_J_Hand_L";
			array2[0, 1] = "cm_J_Hand_R";
			array2[1, 0] = "cf_J_Hand_L";
			array2[1, 1] = "cf_J_Hand_R";
			string[,] array3 = array2;
			for (int i = 0; i < 2; i++)
			{
				_ociChar.hands[i] = UnityEngine.Object.Instantiate<GameObject>(array[i]);
				_ociChar.hands[i].transform.SetParent(_ociChar.charInfo.transform);
				_ociChar.handAnimeCtrl[i] = _ociChar.hands[i].GetComponent<HandAnimeCtrl>();
				GameObject gameObject = _ociChar.charInfo.objBodyBone.transform.FindLoop(array3[sex, i]);
				if (gameObject == null)
				{
					GlobalData.Debug_Log(array3[sex, i]);
				}
				else
				{
					_ociChar.handAnimeCtrl[i].targetRoot = gameObject.transform;
				}
			}
		}

		// Token: 0x06001C0B RID: 7179 RVA: 0x000B4708 File Offset: 0x000B2908
		public static void InitLookAt(OCIChar _ociChar)
		{
			bool flag = _ociChar.oiCharInfo.lookAtTarget == null;
			if (flag)
			{
				_ociChar.oiCharInfo.lookAtTarget = new LookAtTargetInfo(Studio.GetNewIndex());
			}
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(global::Singleton<Studio>.Instance.prefabLookAt);
			GameObject gameObject2 = _ociChar.charInfo.objBodyBone.transform.FindLoop((_ociChar.oiCharInfo.sex != 1) ? "cm_J_Head" : "cf_J_Head");
			if (gameObject2)
			{
				gameObject.transform.SetParent(gameObject2.transform);
				gameObject.transform.localPosition = new Vector3(0f, 0f, 0.25f);
				gameObject.transform.localEulerAngles = Vector3.zero;
			}
			if (flag)
			{
				_ociChar.oiCharInfo.lookAtTarget.changeAmount.pos = gameObject.transform.localPosition;
			}
			GuideObject guideObject = global::Singleton<GuideObjectManager>.Instance.Add(gameObject.transform, _ociChar.oiCharInfo.lookAtTarget.dicKey);
			guideObject.enableRot = false;
			guideObject.enableScale = false;
			guideObject.enableMaluti = false;
			guideObject.scaleRate = 0.5f;
			guideObject.scaleSelect = 0.25f;
			guideObject.parentGuide = _ociChar.guideObject;
			guideObject.changeAmount.OnChange();
			_ociChar.lookAtInfo = new OCIChar.LookAtInfo(guideObject, _ociChar.oiCharInfo.lookAtTarget);
			_ociChar.lookAtInfo.active = false;
			_ociChar.charInfo.neckLookCtrl.SetTarget(Camera.main.transform);
			_ociChar.charInfo.eyeLookCtrl.SetTarget(Camera.main.transform);
		}

		// Token: 0x06001C0C RID: 7180 RVA: 0x000B48B0 File Offset: 0x000B2AB0
		public static void UpdateState(OCIChar _ociChar)
		{
			CharFileStatus charStatus = _ociChar.charStatus;
			for (int i = 0; i < charStatus.clothesState.Length; i++)
			{
				_ociChar.SetClothesState(i, charStatus.clothesState[i]);
			}
			for (int j = 0; j < charStatus.showAccessory.Length; j++)
			{
				_ociChar.ShowAccessory(j, charStatus.showAccessory[j]);
			}
			_ociChar.charInfo.ChangeEyesPtn(charStatus.eyesPtn);
			_ociChar.ChangeEyesOpen(charStatus.eyesOpenMax);
			_ociChar.ChangeBlink(charStatus.eyesBlink);
			_ociChar.charInfo.ChangeMouthPtn(charStatus.mouthPtn);
			_ociChar.ChangeMouthOpen(_ociChar.oiCharInfo.mouthOpen);
			_ociChar.ChangeHandAnime(0, _ociChar.oiCharInfo.handPtn[0]);
			_ociChar.ChangeHandAnime(1, _ociChar.oiCharInfo.handPtn[1]);
			_ociChar.ChangeLookEyesPtn(charStatus.eyesLookPtn, true);
			if (_ociChar.oiCharInfo.eyesByteData != null)
			{
				using (MemoryStream memoryStream = new MemoryStream(_ociChar.oiCharInfo.eyesByteData))
				{
					using (BinaryReader binaryReader = new BinaryReader(memoryStream))
					{
						_ociChar.charInfo.eyeLookCtrl.Load(binaryReader);
					}
				}
				_ociChar.oiCharInfo.eyesByteData = null;
			}
			if (_ociChar.oiCharInfo.neckByteData != null)
			{
				using (MemoryStream memoryStream2 = new MemoryStream(_ociChar.oiCharInfo.neckByteData))
				{
					using (BinaryReader binaryReader2 = new BinaryReader(memoryStream2))
					{
						_ociChar.neckLookCtrl.Load(binaryReader2);
					}
				}
				_ociChar.oiCharInfo.neckByteData = null;
			}
			_ociChar.ChangeLookNeckPtn(charStatus.neckLookPtn);
			for (int k = 0; k < 5; k++)
			{
				_ociChar.SetSiruFlags((CharDefine.SiruParts)k, _ociChar.oiCharInfo.siru[k]);
			}
			if (_ociChar.charInfo.sex == 1)
			{
				_ociChar.SetTearsLv(charStatus.tearsLv);
				_ociChar.SetTuyaRate(_ociChar.oiCharInfo.skinRate);
				_ociChar.SetHohoAkaRate(charStatus.hohoAkaRate);
				_ociChar.ChangeFaceOption(_ociChar.oiCharInfo.faceOption);
			}
		}

		// Token: 0x06001C0D RID: 7181 RVA: 0x000B4B24 File Offset: 0x000B2D24
		private static void AddIKTarget(OCIChar _ociChar, IKCtrl _ikCtrl, int _no, IKEffector _effector, bool _usedRot, Transform _bone)
		{
			GameObject gameObject = AddObjectAssist.AddIKTarget(_ociChar, _ikCtrl, _no, _effector.target, _usedRot, _bone, true);
			_effector.positionWeight = 1f;
			_effector.rotationWeight = ((!_usedRot) ? 0f : 1f);
			_effector.target = gameObject.transform;
		}

		// Token: 0x06001C0E RID: 7182 RVA: 0x000B4B78 File Offset: 0x000B2D78
		private static void AddIKTarget(OCIChar _ociChar, IKCtrl _ikCtrl, int _no, FBIKChain _chain, bool _usedRot, Transform _bone)
		{
			GameObject gameObject = AddObjectAssist.AddIKTarget(_ociChar, _ikCtrl, _no, _chain.bendConstraint.bendGoal, _usedRot, _bone, false);
			_chain.bendConstraint.weight = 1f;
			_chain.bendConstraint.bendGoal = gameObject.transform;
		}

		// Token: 0x06001C0F RID: 7183 RVA: 0x000B4BC0 File Offset: 0x000B2DC0
		private static GameObject AddIKTarget(OCIChar _ociChar, IKCtrl _ikCtrl, int _no, Transform _target, bool _usedRot, Transform _bone, bool _isRed)
		{
			OIIKTargetInfo oiiktargetInfo = null;
			bool flag = !_ociChar.oiCharInfo.ikTarget.TryGetValue(_no, out oiiktargetInfo);
			if (flag)
			{
				oiiktargetInfo = new OIIKTargetInfo(Studio.GetNewIndex());
				_ociChar.oiCharInfo.ikTarget.Add(_no, oiiktargetInfo);
			}
			switch (_no)
			{
			case 0:
				oiiktargetInfo.group = OIBoneInfo.BoneGroup.Body;
				break;
			case 1:
			case 2:
			case 3:
				oiiktargetInfo.group = OIBoneInfo.BoneGroup.LeftArm;
				break;
			case 4:
			case 5:
			case 6:
				oiiktargetInfo.group = OIBoneInfo.BoneGroup.RightArm;
				break;
			case 7:
			case 8:
			case 9:
				oiiktargetInfo.group = OIBoneInfo.BoneGroup.LeftLeg;
				break;
			case 10:
			case 11:
			case 12:
				oiiktargetInfo.group = OIBoneInfo.BoneGroup.RightLeg;
				break;
			}
			GameObject gameObject = new GameObject(((!_target) ? string.Empty : _target.name) + "(work)");
			gameObject.transform.SetParent(_ociChar.charInfo.transform);
			GuideObject guideObject = global::Singleton<GuideObjectManager>.Instance.Add(gameObject.transform, oiiktargetInfo.dicKey);
			guideObject.enableRot = _usedRot;
			guideObject.enableScale = false;
			guideObject.enableMaluti = false;
			guideObject.calcScale = false;
			guideObject.scaleRate = 0.5f;
			guideObject.scaleRot = 0.05f;
			guideObject.scaleSelect = 0.1f;
			guideObject.parentGuide = _ociChar.guideObject;
			guideObject.guideSelect.color = ((!_isRed) ? Color.blue : Color.red);
			OCIChar.IKInfo ikinfo = new OCIChar.IKInfo(guideObject, oiiktargetInfo, _target, gameObject.transform, _bone);
			if (!flag)
			{
				oiiktargetInfo.changeAmount.OnChange();
			}
			_ikCtrl.addIKInfo = ikinfo;
			_ociChar.listIKTarget.Add(ikinfo);
			guideObject.SetActive(false, true);
			return gameObject;
		}
	}
}
